<template>
  <div class="container">
    <div class="search-box">
      <el-input placeholder="请输入内容" class="input-box" v-model="searchinput"></el-input>
      <el-button @click="clearList">清除</el-button>
      <el-button type="primary" @click="searchList">搜索</el-button>
    </div>
    <el-table border :data="list" style="width: 100%">
      <el-table-column type="index" label="任务编号" width="80"> </el-table-column>
      <el-table-column prop="name" label="任务名称"> </el-table-column>
      <el-table-column prop="des" label="任务描述"> </el-table-column>
      <el-table-column fixed="right" label="操作" width="180">
        <template #default="{ row }">
          <el-button size="mini" style="margin-right: 10px" @click="editList(row.id)"
            >编辑</el-button
          >
          <el-popconfirm
            confirmButtonText="好的"
            cancelButtonText="不用了"
            icon="el-icon-info"
            iconColor="red"
            title="确定删除任务吗？"
            @confirm="delList(row.id)"
          >
            <template #reference>
              <el-button size="mini" type="danger">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div class="pageContainer">
      <el-pagination layout="prev, pager, next" :total="50" @current-change="pageChange">
      </el-pagination>
      <el-button size="small" plain @click="add">+ 新增</el-button>
      <el-dialog title="提示" :visible="dialogVisible" :before-close="closeDialog" width="30%">
        <span>提示</span>
        <template #default>
          <el-form ref="Form" :model="addForm" :rules="rules" label-width="100px">
            <el-form-item label="任务名称" prop="name">
              <el-input v-model="addForm.name" />
            </el-form-item>
            <el-form-item label="任务描述" prop="des">
              <el-input v-model="addForm.des" />
            </el-form-item>
          </el-form>
        </template>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="confirm">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { v4 as uuidv4 } from 'uuid'
import {
  fetachDetailList,
  fetachGetList,
  fetachAddList,
  fetachEditList,
  fetachDelList,
  fetachSerachList
} from '@/api/index.js'
export default {
  data() {
    return {
      list: [],
      query: {
        _page: 1, // 当前第几页
        _limit: 5 // 每页条数
      },
      total: 0,
      dialogVisible: false,
      searchinput: '',
      addForm: {
        name: '',
        des: '',
        id: ''
      },
      rules: {
        name: [{ required: true, message: '任务名称不能为空', trigger: 'blur' }],
        des: [{ required: true, message: '任务描述不能为空', trigger: 'blur' }]
      }
    }
  },
  methods: {
    async getList() {
      const res = await fetachGetList(this.query)
      this.list = res.data
    },
    async editList(id) {
      const res = await fetachDetailList(id)
      this.addForm = res.data
      this.dialogVisible = true
    },
    // 页码改变的时候，触发的事件
    pageChange(newPage) {
      this.query._page = newPage
      this.getList()
    },
    add() {
      this.dialogVisible = true
    },
    closeDialog() {
      this.dialogVisible = false
    },
    // 删除
    async delList(id) {
      await fetachDelList(id)
      this.$message.success('删除成功')
      // 更新列表
      this.getList()
    },
    // 搜索
    async searchList() {
      const res = await fetachSerachList(this.searchinput)
      this.list = res.data
    },
    // 清除
    async clearList() {
      this.searchinput= ''
      this.getList()
    },
    // 兜底校验
    confirm() {
      this.$refs.Form.validate(async valid => {
        if (valid) {
          if (this.addForm.id) {
            await fetachEditList(this.addForm)
          } else {
            this.addForm.id = uuidv4()
            await fetachAddList(this.addForm)
          }
          // 提示
          // 关闭弹框
          this.$message.success(`${this.addForm.id ? '编辑成功' : '新增成功'}`)
          this.dialogVisible = false
          this.getList()
          // 清空数据
          this.addForm = {
            name: '',
            des: '',
            id: ''
          }
        }
      })
    }
  },
  created() {
    this.getList()
  }
}
</script>

<style scoped lang="less">
.container {
  width: 800px;
  margin: 0 auto;
}
.search-box {
  display: flex;
  margin-bottom: 10px;
  .input-box {
    margin-right: 20px;
  }
}
.pageContainer {
  margin-top: 10px;
  display: flex;
  justify-content: space-between;
}
</style>
